﻿using Microsoft.AspNetCore.Mvc;
using OrderSystemApi.Pojo;
using OrderSystemApi.Services;
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;


namespace OrderSystemApi.Controllers
{
    [Route("orderApi")]
    [ApiController]
    public class OrderController : ControllerBase
    {
        private readonly OrderService _orderService;
        private readonly IdWorker worker = new IdWorker(1, 1);
        public OrderController(OrderService orderService)
        {
            _orderService = orderService;
        }
        // GET: orderApi/getAll
        // 查询所有订单
        [HttpGet("getAll")]
        public IEnumerable<Order> Get()
        {
            return _orderService.getOrderList();
        }

        // GET orderApi/getById/5
        // 根据id查询订单
        [HttpGet("getById/{id}")]
        public Order GetById(long id)
        {
            return _orderService.getOrderById(id);
        }

        // GET orderApi/getByName/小红
        // 根据姓名查询订单
        [HttpGet("getByName/{name}")]
        public IEnumerable<Order> GetByName(string name)
        {
            return _orderService.getOrderByName(name);
        }

        // POST orderApi/add
        // 增加订单
        [HttpPost("add")]
        public Dictionary<string, Object> Post([FromBody] Order order)
        {
            order.Id = worker.NextId();
            Dictionary<string, Object> result = new()
            {
                { "data", _orderService.addOrder(order) }
            };
            return result;
        }

        // PUT orderApi/update/5
        // 修改订单
        [HttpPut("update/{id}")]
        public Dictionary<string, Object> Put(long id, [FromBody] Order order)
        {
            order.Id = id;
            Dictionary<string, Object> result = new()
            {
                { "data", _orderService.updateOrder(id, order) }
            };
            return result;
        }

        // DELETE orderApi/delete/5
        // 删除订单
        [HttpDelete("delete/{id}")]
        public Dictionary<string, Object> Delete(long id)
        {
            Dictionary<string, Object> result = new()
            {
                { "data", _orderService.deleteOrder(id) }
            };
            return result;
        }
    }
}
